using System;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using <<|=HabaneroUILibrary|>>;
using <<|=BOProjectName|>>;

namespace <<|=ProjectName|>>
{
    /// <summary>
    /// A read-only grid control used for adding and editing <<|=ClassName+|>>.
    /// </summary>
    public class <<|=ControlName|>> : <<|=UserControlType|>>, IFormControl
    {
        private readonly IControlFactory _controlFactory;

        public <<|=ClassName+|>>GridControl(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            _controlFactory = controlFactory;

            // Create the read-only grid
            IReadOnlyGridControl <<|=#ClassName|>>GridControl = _controlFactory.CreateReadOnlyGridControl();
            
            // Used to specify a custom UI def
            <<|=#ClassName|>>GridControl.Initialise(ClassDef.Get<<<|=ClassName|>>>(), "<<|=UIDefName|>>");

            // Load the collection of <<|=ClassName+|>>.  You can set the criteria and order by values here.
            BusinessObjectCollection<<<|=ClassName|>>> <<|=#ClassName|>>Collection = Broker.GetBusinessObjectCollection<<<|=ClassName|>>>("", "");
            <<|=#ClassName|>>GridControl.SetBusinessObjectCollection(<<|=#ClassName|>>Collection);

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            <<|=#ClassName|>>GridControl.FilterControl.Visible = false;
            //<<|=#ClassName|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //<<|=#ClassName|>>GridControl.Buttons.ShowDefaultDeleteButton = true;

            // Add the grid to the control so that it fills the control
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(<<|=#ClassName|>>GridControl, BorderLayoutManager.Position.Centre);
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}